package com.zwz.utils;

public class MessageUtil {

    public static String boxCode;
    public static String guanhao = "1";
    private static int inferCount = 0;

    public static String GuanhaoCorrect(String message) {
        if (guanhao.length() < 2) {
            guanhao = "0" + guanhao;
        }
        int splitIndex = message.indexOf("|");
        String QRCode = message.substring(0, splitIndex);
        String Code = message.substring(splitIndex + 1);

        if (!Code.equals("noread")) {
            String part1 = Code.substring(0, 12);
            String receiveGuanhao = Code.substring(12, 14);
            String part3 = Code.substring(14, 16);
            if (receiveGuanhao != guanhao) {
                return QRCode + "|" + part1 + guanhao + part3;
            }
        }
        return message;
    }


    public static String BoxCodeInfer(String message) {
        if (message.endsWith("\r\n"))
        {
            message = message.replace("\r\n", "");
        }
        int splitIndex = message.indexOf("|");
        String QRCode = message.substring(0, splitIndex);
        String Code = message.substring(splitIndex + 1);
        System.out.println(QRCode);
        System.out.println(Code);
        if (QRCode.equals("noread"))
        {
            return message;
        }
        if (Code.equals("noread") && boxCode != null)
        {
            inferCount++;
            System.out.println("未读取，推测字符为：" + boxCode);
            System.out.println("累计推测次数：" + inferCount);
            return QRCode + "|" + boxCode;
        }
        if (!Code.equals("noread"))
        {
            System.out.println("已读取");
            boxCode = Code;
            return message;
        }
        return message;
    }
}
